package com.it.impl;

import com.it.PlateService;
import com.it.dao.ArticleMapper;
import com.it.dao.PlateMapper;
import com.it.pojo.Article;
import com.it.pojo.Category;
import com.it.pojo.Plate;
import com.it.pojo.vo.category.AdminCategoryVO;
import com.it.pojo.vo.plate.PlateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class PlateServiceImpl implements PlateService {

    @Resource
    private PlateMapper plateMapper;

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public List<PlateVO> listPlates() {
        return copyList(plateMapper.selectList(null));

    }

    @Override
    public List<PlateVO> list() {
        List<PlateVO> plateVOS = copyList(plateMapper.selectList(null));
        List<PlateVO> list = plateVOS.stream().map(plateVO -> {
            Integer total = articleMapper.getArticleTotalByPlate(plateVO.getPlateId()) == null
                    ? 0 : articleMapper.getArticleTotalByPlate(plateVO.getPlateId());
            plateVO.setTotal(total);
            return plateVO;
        }).collect(Collectors.toList());
        return list;
    }

    private PlateVO copy(Plate plate) {
        PlateVO plateVO = new PlateVO();
        BeanUtils.copyProperties(plate, plateVO);
        return plateVO;
    }

    private List<PlateVO> copyList(List<Plate> plateList) {
        List<PlateVO> list = new ArrayList<>();
        for (Plate plate : plateList) {
            list.add(copy(plate));
        }
        return list;
    }
}
